<template>
	<view>
		<view class="goods-list">
			<view v-for="(goods, i) in goodsList" :key="i" @click="goDetail(goods)">
				<my-goods :goods="goods"></my-goods>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				queryObj: {
					cid: '',
					query: '',
					pagenum: 1,
					pagesize: 10
				},
				goodsList: [],
				total: 0,
				isloaded: false
				
			};
		},
		onLoad(option) {
			this.queryObj.query = option.query || ''
			this.queryObj.cid = option.cid || ''
			
			this.getGoodsList()
		},
		methods: {
			// 获取商品列表数据
			async getGoodsList(cb) {
				this.isloaded = true
				const {data: res} = await uni.$http.get('/api/public/v1/goods/search', this.queryObj)
				this.isloaded = false
				
				// 数据请求成功后 关闭下拉加载
				cb && cb()
				
				if (res.meta.status !== 200) {
					uni.$showMsg()
					this.isloaded = true
					return
				}
				this.goodsList = [...this.goodsList, ...res.message.goods]
				this.total = res.message.total
			},
			
			// 跳转详情页
			goDetail(goods) {
				uni.navigateTo({
					url: '/subpkg/goods_detail/goods_detail?goods_id=' + goods.goods_id
				})
			}
		},
		onReachBottom() {
			if (this.queryObj.pagenum * this.queryObj.pagesize >= this.total) return uni.$showMsg('数据加载完毕！')
			// 节流阀
			if (this.isloaded) return 
			// 上拉加载更多数据
			this.queryObj.pagenum++
			this.getGoodsList()
		},
		onPullDownRefresh() {
			// 下拉重置数据
			this.goodsList = []
			this.total = 0
			this.isloaded = false
			this.queryObj.pagenum = 1
			
			// 重新获取数据
			this.getGoodsList(() => uni.stopPullDownRefresh())
		}
	}
</script>

<style lang="scss">

</style>
